
/*******************************************************************************
*																			   *
*  	PURPOSE:  			Bootstraps the moments used for the estimation of the  *
						model						   						   *
*																			   *
*  	Last update:  		October 2024										   *
*																			   *
********************************************************************************
						
	** REQUIRES:   		"${DATADIR}/data_supervisors.dta"
						"${DATADIR}/data_workers.dta"
						"${DATADIR}/data_households.dta"
						
	** GENERATES:   	"${DATADIR}/data_all_final.dta"				
							
	** SECTIONS:   		0. Settings
	   (in bookmarks)		0.a	Standardize settings and programs
							0.b	Set folder path globals
							0.c Data settings, macros, and tempfiles
						1. Bootstrap


*******************************************************************************/
**# 	0. Settings				 	
*******************************************************************************/
**## 	0.a Standardize settings and programs
/*----------------------------------------------------------------------------*/
{
	
	* Preamble
	clear 	all 
	set		maxvar 30000
	set 	matsize 10000
	set 	more off
	set 	varabbrev on
	set 	scheme s2color
	
}
	
/*----------------------------------------------------------------------------*/
**## 	0.b Set folder path globals
/*----------------------------------------------------------------------------*/

global path " " // Set this to the path for the replication folder on your machine
	
	global 		   DATADIR 	"${path}/Data"
	global 		 TABLESDIR 	"${path}/Results/Tables"
	global 		FIGURESDIR 	"${path}/Results/Figures"
	global 		 BOOTSTRAP 	"${path}/Do-files/matlab/results"
	
	cd 		"${TABLESDIR}"
	
/*----------------------------------------------------------------------------*/
**## 	0.c Data settings, macros, and tempfiles
/*----------------------------------------------------------------------------*/	
{
	
	* Load the dataset
	use 		"${DATADIR}/data_all_final.dta", clear
	
	* Macros for regressions  
	global 	      controls 	i.district i.distance_abvmedian	i.cluster_size
							
	global		 controls1	district distance_abvmedian	cluster_size
																					
	* Set table general formatting options for [outreg2]
	global	 outreg0ptions  nor2 adec(3) bdec(3) nocons excel label
			
	global 		  boot_N = 	500  // number of boostrap replications
	global 		   var_N = 	2  // number of variables
	global 		   row_N = 	$var_N*8
	
	gen 			   N =	_n

	set 			  seed 	2222
		
}

********************************************************************************
**# 	1. Bootstrap			 	
*******************************************************************************/

	* Select variables
	gen 	control_p4p = supervisor == 0 & worker == 0 & shared == 0

	keep 	HH_accomp_visits_E worker supervisor shared control_p4p 			///
			HH_tot_visits_month_vill_E  WORKER_id_bootstrap

	rename 	control_p4p control

	local 	outcomes "HH_accomp_visits_E HH_tot_visits_month_vill_E"
	local 	k = 0
	
	foreach var of local outcomes {
		
	local 	k =`k' + 1
	gen 	y`k' = `var'

	}

	keep 	y1-y${var_N}  worker supervisor shared control WORKER_id_bootstrap
	lab var y1 "% HH visits in which health worker was accompanied by supervisor"
	lab var y2 "Total number of Worker Visits per month"
	su y1 if worker ==1 
	su y1 if supervisor ==1 
	su y1 if shared ==1 
	su y1 if control ==1
	su y2 if worker ==1 
	su y2 if supervisor ==1 
	su y2 if shared ==1 
	su y2 if control ==1
	
	codebook WORKER_id_bootstrap
	
	sort WORKER_id_bootstrap

	** BOOTSTRAP
	local 	k=0
	forvalues  y =1(1)$var_N   {
		
	display "Variable y`y'"
	local 	k=`k'+1

	local 	label`k': variable label y`y'
	
	*mean
	foreach x in worker supervisor shared control {
	
	local 	j = `j'+1
	bootstrap meany`y'_`x'=r(mean), 											///
			saving("${BOOTSTRAP}/rawbootb_y`y'_`x'", replace) reps($boot_N) 	///
			seed(2222) noisily: summarize y`y' if `x'==1

	matrix B=r(table)
	/* obs */ local C1R`j'= e(N)
	/* mean */ local C2R`j': di %12.7fc B[1,1]
	/* Bootstrap var */ local C3R`j': di %12.7fc B[2,1]^2
	/* INDEX */ local C4R`j'=`j'
	/* Unit */ local C5R`j'="Worker"
	
	}

	*variance
	foreach x in worker supervisor shared control {
	local 	j = `j'+1
	bootstrap vary`y'_`x'=r(Var), reps($boot_N) seed(2222) 						///
			noisily: summarize y`y' if `x'==1
	matrix 	B=r(table)
	/* obs */ local C1R`j'= e(N)
	/* mean */ local C2R`j': di %12.7fc B[1,1]
	/* Bootstrap var */ local C3R`j': di %12.7fc B[2,1]^2
	/* INDEX */ local C4R`j' = `j'
	/* Unit */ local C5R`j' = "Worker"
	
	}
	
	}

	** Create new data with the bootstrap estimation:

	preserve
	clear 	all
	set 	obs $row_N
	gen 	Moment = ""
	gen 	Observations =.
	gen 	Observed_mean =.
	gen 	Variance_Bootstrap =.
	gen 	Indicator =. 
	gen 	Unit = ""

	forval 	j=1/$row_N {
		
	replace Observations=`C1R`j'' in `j'
	replace Observed_mean=`C2R`j'' in `j'
	replace Variance_Bootstrap=`C3R`j'' in `j'
	replace Indicator=`C4R`j'' in `j'
	replace Unit="`C5R`j''" in `j'
	
	}

	local 	i = 0
	
	foreach k of numlist 1 9 17 25 33 41 {
		
	local 	i=`i'+1
	local 	k1=`k'+1
	local 	k2=`k1'+1
	local 	k3=`k2'+1
	local 	k4=`k3'+1
	local 	k5=`k4'+1
	local 	k6=`k5'+1
	local 	k7=`k6'+1

	replace Moment="Mean `label`i'' in Worker Incentives Treatment" 			///
			if Indicator==`k'
	replace Moment="Mean `label`i'' in Supervisor Incentives Treatment" 		///
			if Indicator==`k1'
	replace Moment="Mean `label`i'' in Shared Incentives Treatment" 			///
			if Indicator==`k2'
	replace Moment="Mean `label`i'' in Control Group" 							///
			if Indicator==`k3'
	replace Moment="Variance `label`i'' in Worker Incentives Treatment" 		///
			if Indicator==`k4'
	replace Moment="Variance `label`i'' in Supervisor Incentives Treatment" 	///
			if Indicator==`k5'
	replace Moment="Variance `label`i'' in Shared Incentives Treatment" 		///
			if Indicator==`k6'
	replace Moment="Variance `label`i'' in Control Group" 						///
			if Indicator==`k7'
			
	}

	drop 	Indicator
	export 	excel using "${BOOTSTRAP}/allboot.xlsx", replace firstrow(variables) 
	
	restore
	clear

	set 	obs  ${boot_N}
	gen 	sim = _n
	tempfile tempfile1
	save 	`tempfile1', replace
	clear
	local 	count = 1
	
	foreach y in  1 2  {
		
	foreach x in   worker supervisor shared control {
		
	use  	"${BOOTSTRAP}/rawbootb_y`y'_`x'.dta
	gen 	sim = _n
	rename 	meany`y'_`x' m`count'
	merge 	1:1 sim using `tempfile1'
	drop 	_merge
	save 	`tempfile1', replace
	clear
	local 	count = `count'+ 1
	
	}
	
	}
		
	use 	`tempfile1'

	order sim m1 m2 m3 m4 m5 m6 m7 m8 

	save 	"${BOOTSTRAP}/allboot.dta", replace
	drop 	sim
	outsheet using "${BOOTSTRAP}/allboot.csv", comma non replace

